Aller au contenu principal

Postgresql Docker Setup

Install Docker

curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER

Log out and back in, then verify:

docker --version

Postgres and Pgadmin docker compose

The following docker compose creates postgres 16 container on 5432 and pgadmin4 on port 5050. The default username and password for postgres database is postgres and postgres respectively.

Open Pgadmin4 by entering localhost:5050 or <remote_ip>:5050. Then use the following credential to connect to postgres database:

host: localhost or postgres or <inspect the docker container for private ip within the bridge>
username: postgres
password: postgres
port: 5432
services:
postgres:
image: postgres:16
container_name: local_postgres
restart: unless-stopped

environment:
POSTGRES_USER: postgres # change as you like
POSTGRES_PASSWORD: postgres # change as you like
POSTGRES_DB: postgres # initial DB

volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"

healthcheck:
test: ["CMD-SHELL", "pg_isready -U useName -d mydb || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s

networks:
- backend

pgadmin:
image: dpage/pgadmin4:latest
container_name: local_pgadmin
restart: unless-stopped

environment:
PGADMIN_DEFAULT_EMAIL: [email protected] # change
PGADMIN_DEFAULT_PASSWORD: ExampleAdmn123 # change
PGADMIN_CONFIG_SERVER_MODE: "False" # single-user desktop mode

volumes:
- ./pgadmin-data:/var/lib/pgadmin

# Also bind pgAdmin to a specific host IP
# For "localhost only" access:
ports:
- "5050:80"

depends_on:
postgres:
condition: service_healthy

healthcheck:
test: ["CMD", "wget", "-O", "-", "http://localhost:80/misc/ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s

networks:
- backend

networks:
backend:
driver: bridge

remarque

Connect to remote database using terminla

psql -h <remote_host> -p <port> -U <username> -d <target_db>

If you are using the above setup, the default port is 5432, username is postgres, target_db is postgres.

To restore a database on remote host use the following command,

pg_restore -h <remote_host> -U <username> -d <target_db_name> -v <path_to_dump_file>